import Vue from 'vue'
import Router from 'vue-router'
import home from '@/components/home'
import Login from '../pages/login/'

Vue.use(Router);

const router = new Router({
    routes: [{
            path: '/',
            name: 'home',
            component: home,
            meta: {
                isLogin: true
            }
        },
        { path: '/login', name: 'login', component: Login },

    ]
});

router.beforeEach((to, from, next) => {
    if (to.matched.some(res => res.meta.isLogin)) {
        const userName = localStorage.getItem('userName');
        console.log(userName)
        if (!userName) {
            next({
                path: '/login',
            })
        } else {
            next();
        }
    } else {
        next()
    }
})


export default router;